package utilities;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Scanner;

public class FileTools {

    //Encodage à utiliser pour la lecture des fichiers
    public static final Charset ENCODING = StandardCharsets.ISO_8859_1;

    public static String readTextFile(String fileFullPath) throws IOException {
        String fileContent = "";

        Path path = Paths.get(fileFullPath);
        try (Scanner scanner = new Scanner(path, ENCODING.name())) {
            while (scanner.hasNextLine()) {

                fileContent += scanner.nextLine();
            }
        }

        return fileContent;
    }

    public static String[] splitOnSpaces(String stringToSplit) {
        /**
         * \s - Matches any white-space character.
         */
        return stringToSplit.split("\\s+");
    }

    /*
     * Retourne la liste des fichiers trouvés
     */
    public static void findFiles(String folderPath, ArrayList<File> foundFiles) {
        
        File root = new File(folderPath);
        File[] list = root.listFiles();

        for (File f : list) {
            if (f.isDirectory()) {
                findFiles(f.getAbsolutePath(), foundFiles);
                //System.out.println("Dossier:" + f.getAbsoluteFile());
            } else {
                foundFiles.add(f);
                //System.out.println("Fichier:" + f.getAbsoluteFile());
            }
        }
    }
}
